<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Gallery/classes/PhpFox_ComponentTop.class.php, $Author:  $ */



/** Component for showing top viewed images
 *
 * @version $Id: PhpFox_ComponentTop.class.php 467 2008-06-10 20:44:39Z phpfoxsv $
 * @package module.gallery
 */
class PhpFox_Mod_Gallery_ComponentTop extends BaseComponent
{
    function process()
    {
        $oGalleryItem = $this->_oModule->getItem('Gallery');
        /* @var $oGalleryItem PhpFox_Mod_Gallery_ItemGallery */
        $oGalleryService = $this->_oModule->getService('Gallery');
        /* @var $oGalleryService PhpFox_Mod_Gallery_ServiceGallery */
        $oSecurityService = App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oReq = &App::getRequest();
        $oTpl = $this->_oModule->getTemplate();
        App::loadClass('Sorter');
        App::loadClass('Filter');

        $oSecurityService->checkPerm('gallery.rate', array('full', 'read_only'));
        $oSecurityService->checkAccess('rate');
        $aFields = array
        (
        	'rating',
        	'view',
        );
        $aDefSort = array($oReq->get('field', 'rating') => 'desc');
        $oSorter = &new Sorter($aFields, $aDefSort);
        $aCategories = array();
        $aCategories[''] = App::format('option.select');
        foreach (App::getOptions('image_cat') as $sCategoryId => $sCategoryText)
        {
            $aCategories[$sCategoryId] = $sCategoryText;
        }
        $aCond = array
        (
        	'category' => array
        	(
        		'value'  => '=',
        		'select' => $aCategories,
        	),
        );
        $oFilter = &new Filter('catrate', $aCond);
        $aCond = array();
        foreach ($oFilter->aConditions as $sCond)
        {
        	$sCond = preg_replace('#{\#(.*)}#i', ' m.\\1', $sCond);
            $aCond[] = $sCond;
        }
        if ($oSorter->sSortField == 'rating')
        {
            $aCond[] = ' votes > 3';
        	$aCond[] = ' m.rating <> 0';            
        }
        if ($oSorter->sSortField == 'view')
        {
            $aCond[] = ' m.view <> 0';
        }
        $aCond[] = ' m.allow = ""';
        $aCond[] = ' m.who_view = ""';
        $sSort = $oSorter->getOrder().(($oSorter->sSortField == 'rating') ? ', votes DESC' : '');

		$sConditions = "";
		if ( isset($aCond) && count($aCond) > 0 )
		{
        	$sConditions = " AND " . implode(" AND ", $aCond);
        }

		$aItems = $this->oDatabase->getRows("
			SELECT 
				m.id, m.text, m.time, u.user, m.view, m.rating, m.votes
			FROM ". App::getT('main') ." AS m
				LEFT JOIN ". App::getT('user') ." AS u ON(m.user_id = u.id)
			WHERE 1 ". $sConditions ."
			ORDER BY ". $sSort ."			
			LIMIT 0,". App::getParam('public.top_size') ."
		");
		
        $oTpl->assign(array(
        	'aItems'      => $aItems,
        	'aFilter'     => $oFilter->aHtml,
        	'sFilterName' => 'catrate',
        	'sGImageUrl'  => App::getParam('gallery.pic.url'),
        ));
        $aOut = array();
        $aOut['main']   = $oTpl->fetch('Top.html');
        $aOut['filter'] = $oTpl->fetch('GroupFilter.html');
        return $aOut;
    }
}
?>